<?php
namespace app\examScore\controller;
use app\common\Base;
use think\Db;
//--优异奖
class ExamExcellentPrize extends Base {
	
	public function index() {
		$fields = "班级,班主任,综合实力名次,综合实力得分,培优名次,培优得分,得分合计,总名次,实验班,是否学习先进班";
		$list = explode(",", $fields);
		$this->assign('list',$list);
		return $this->fetch();
	}

	public function pageQuery() {
		$examId = input("examId");
		if(!$examId) return [];
		$page = input("page");
		$rows = input("rows");
		$where["考试ID"] = $examId;
		$subSql = Db::table("VIEW学习先进班初一")
				  ->field("实验班,count(*)/2 as 班数半数")
				  ->where($where)
				  ->group("实验班")
				  ->buildSql();
		$result = Db::table("VIEW学习先进班初一 a")
				  ->join([$subsql=>'b'],'a.实验班=b.实验班',"LEFT")
				  ->field("班级,班主任,综合实力名次,综合实力得分,培优名次,培优得分,得分合计,总名次,a.实验班,case when 总名次<=班数半数 then '学习先进班' else '' end as 是否学习先进班")
				  ->where($where)
				  ->order("实验班,班级")
				  ->page($page, $rows)
				  ->select();
		return selectResult($result);
	}

	public function exportExcel() {
		$examId = input("examId");
		if(!$examId) return [];
		$where["考试ID"] = $examId;
		$subSql = Db::table("VIEW学习先进班初一")
				  ->field("实验班,count(*)/2 as 班数半数")
				  ->where($where)
				  ->group("实验班")
				  ->buildSql();
		$result = Db::table("VIEW学习先进班初一 a")
				  ->join([$subsql=>'b'],'a.实验班=b.实验班',"LEFT")
				  ->field("班级,班主任,综合实力名次,综合实力得分,培优名次,培优得分,得分合计,总名次,a.实验班,case when 总名次<=班数半数 then '学习先进班' else '' end as 是否学习先进班")
				  ->where($where)
				  ->order("实验班,班级")
				  ->select();
		$result = selectResult($result);
		//excel表头内容
		$header = array('班级'=>'班级','班主任'=>'班主任','综合实力名次'=>'综合实力名次','综合实力得分'=>'综合实力得分','培优名次'=>'培优名次','培优得分'=>'培优得分','得分合计'=>'得分合计','总名次'=>'总名次','是否学习先进班'=>'是否学习先进班');
		Excel::toExcel($result["rows"],'学习先进班初一',$header);
		exit;
	}
}
